import React, { useEffect } from 'react'
import { Button, Card, Checkbox, Form, Input, message, Table } from 'antd';
import style from '../index.module.css'
import { addHospitalRequest, getHospitalFromIdRequest,updateHospitalRequest } from '@api/hospital/hospitalSet'
import { AddHospitalParamsType } from '@/api/hospital/model/hospitalSetType';
import { useNavigate, useParams } from 'react-router-dom';

export default function AddOrUpdateHospital() {
    const [form] = Form.useForm()
    //编程式路由
    const navigate = useNavigate()
    //获取id
    const { id } = useParams()
    //初始化时获取当前修改医院的信息
    useEffect(() => {
        if (id) {
            const getHosSetFromId = async () => {
                const re = await getHospitalFromIdRequest(id)
                form.setFieldsValue(re)
            }
            getHosSetFromId()
        }
    })
    //新增医院
    const finishHandle = async (fieldValue: AddHospitalParamsType) => {
        if (id) {
            await updateHospitalRequest({...fieldValue,id})
            navigate('/syt/hospital/hospitalSet')
            message.success('修改医院成功')
        } else {
            await addHospitalRequest(fieldValue)
            navigate('/syt/hospital/hospitalSet')
            message.success('新增医院成功')
        }
    }

    return (
        <Card>
            <Form
                name="basic"
                labelCol={{ span: 2 }}
                wrapperCol={{ span: 30 }}
                className={style.outer}
                onFinish={finishHandle}
                form={form}
            >
                <Form.Item
                    name="hosname"
                    label='医院名称:'
                    rules={[{ required: true, message: '请输入医院名称' ,
                    transform(value){
                        return value.trim()
                    }
                }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    name="hoscode"
                    label='医院编号:'
                    rules={[{ required: true, message: '请输入医院编号' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="apiUrl"
                    label='api基础路径:'
                    rules={[{ required: true, message: '请输入api基础路径' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="contactsName"
                    label='联系人姓名:'
                    rules={[{ required: true, message: '请输入联系人姓名' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="contactsPhone"
                    label='联系人手机:'
                    rules={[{ required: true, message: '请输入联系人手机' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 2, span: 16 }}>
                    <Button type="primary" htmlType="submit">
                        保存
                    </Button>
                    <Button type="default" htmlType="reset" className={style.mt} onClick={() => { navigate(-1) }}>
                        返回
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    )
}
